# author       :  Woo_home
# create_time  :  2020/2/18 23:40

from openpyxl import load_workbook

class DoExcel:
    def __init__(self,filename):
        '''
        :param filename: excel文件名
        '''
        self.file = filename
        self.wk = load_workbook(self.file)

    def do_excel(self,sheetname):
        '''
        :param sheetname: 工作簿名称
        :return:
        '''
        sheet = self.wk[sheetname]
        max_row = sheet.max_row #最大行
        max_column = sheet.max_column #最大列
        data = [] #定义一个空列表,用于存储所有数据
        for r in range(2,max_row+1):
            subdata = {} #定义一个字典,用于存储每行数据
            for c in range(1,max_column+1):
                key = sheet.cell(1,c).value  #取第一行表头数据
                subdata[key] = sheet.cell(r,c).value #字典格式，表头作为key
            data.append(subdata)
        return data

if __name__ == '__main__':
    file_name = "words.xlsx"
    # 获取所有数据
    datas = DoExcel(file_name).do_excel('Sheet1')
    # 获取数据的长度
    for i in range(0, len(datas)):
        # 遍历数据
        for data in datas:
            # 定义一个空的字典
            topic = {}
            # 讲遍历的数据存储到字典中
            topic['topic'] = data['Topic']
            topic['A'] = data['A']
            topic['B'] = data['B']
            topic['C'] = data['C']
            topic['D'] = data['D']
            topic['answer'] = data['answer']

            # 打印题目和选项
            print(topic['topic'])
            print("A、" + topic['A'])
            print("B、" + topic['B'])
            print("C、" + topic['C'])
            print("D、" + topic['D'])

            # 用户输入
            result = input("请输入正确答案：\n")
            # 答案不区分大小写
            if result == topic['answer'] or result.upper() == topic['answer']:
                print("回答正确")
                print("----------------------------------------")
            else:
                print("回答错误，正确答案为：",topic['answer'])
                print("-----------------------------------------")
